package com.yunjijia.controller;

import com.yunjijia.pojo.House;
import com.yunjijia.service.HouseService;
import com.yunjijia.utils.PageResult;
import com.yunjijia.utils.R;
import com.yunjijia.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/house")
public class HouseController {


    @Autowired
    private HouseService houseService;

    /**
     * id单个查询
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectById")
    public R selectById(long id) {
        House house = houseService.selectByid(id);
        return new R(true, StatusCode.OK, "查询成功");
    }

    /**
     *
     *查所有
     * @return
     */
    @RequestMapping("/selectAll")
    public R selectAll() {
        List<House> list = houseService.selecrAll();
        return new R(true, StatusCode.OK, "查询成功", list);
    }


    /**
     * 保存
     * @param house
     * @return
     */
    @RequestMapping("/save")
    public R save(@RequestBody House house) {

        house.setCreateTime(new Date());
        houseService.save(house);
        return new R(true, StatusCode.OK, "添加成功");

    }


    /**
     * 单个id删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/del")
    public R del(long id) {
        houseService.del(id);
        return new R(true, StatusCode.OK, "删除成功");
    }


    /**
     * 批量id删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/dels")
    public R dels(long[] ids) {
        for (long id : ids) {
            houseService.del(id);
        }
        return new R(true, StatusCode.OK, "删除成功");
    }


    /**
     * 修改数据回显
     */
    @RequestMapping("/findOne")
    public R findOne(long id) {
        House house = houseService.selectByid(id);
        return new R(true, StatusCode.OK, "查询成功",house);
    }

    /**
     * 数据修改
     *
     * @param house
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody House house) {
        houseService.update(house);
        return new R(true, StatusCode.OK, "修改成功");
    }


    /**
     * 无条件分页查询
     *
     * @return
     */
    @RequestMapping("/searchAll")
    public PageResult search(int page, int size) {
        return houseService.search(page, size);

    }

    /**
     * 多条件分页查询
     * @param page
     * @param size
     * @param house
     * @return
     */
    @RequestMapping("/search")
    public PageResult requirementSearch(int page, int size, @RequestBody House house) {
            return houseService.requirementSearch(page, size, house);
    }

    /**
     * 城市选择下拉框
     * @return
     */
    @RequestMapping("/selectHouseCityList")
    public R selectHouseCityList() {
        List<Map> List = houseService.selectHouseCityList();
        return new R(true, StatusCode.OK, "查询成功", List);
    }


    /**
     * 公司选择下拉框
     * @return
     */
    @RequestMapping("/selectHouseBuiltList")
    public R selectHouseBuiltList() {
        List<Map> List = houseService.selectHouseBuiltList();
        return new R(true, StatusCode.OK, "查询成功", List);
    }

    /**
     * 小区选择下拉框
     */
    @RequestMapping("/selectHouseCommunitynameList")
    public R selectHouseCommunitynameList()
    {
        List<Map> List = houseService.selectHouseCommunitynameList();
        return new R(true, StatusCode.OK, "查询成功", List);
    }


    /**
     * 面积选择下拉框
     */
    @RequestMapping("/selectHouseAreaList")
    public R selectHouseAreaList()
    {
        List<Map> List = houseService.selectHouseAreaList();
        return new R(true, StatusCode.OK, "查询成功", List);
    }

    /**
     * 卧室选择下拉框
     */
    @RequestMapping("/selectHousebedsList")
    public R selectHousebedsList()
    {
        List<Map> List = houseService.selectHousebedsList();
        return new R(true, StatusCode.OK, "查询成功", List);
    }
}
